package api

import (
	"blog/library"
	"github.com/astaxie/beego"
)

type Api struct {
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
	Message string      `json:"message"`
	Count   int64       `json:"count"`
}

var redis library.Redis

type baseController struct {
	beego.Controller
}

func (self *baseController) success(data interface{}, message ...interface{}) {
	var api Api
	api.Data = data
	api.Message = "操作成功"
	if len(message) > 0 {
		api.Message = message[0].(string)
		if len(message) > 1 {
			api.Count = message[1].(int64)
		}
	}

	api.Code = 1
	self.baseReturn(api)
}

func (self *baseController) error(message string) {
	var api Api
	api.Message = message
	api.Code = 0
	self.baseReturn(api)
}

func (self *baseController) baseReturn(api Api) {
	self.Data["json"] = api
	self.ServeJSON()
	self.StopRun()
}
